package com.attack.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.attack.manager.InterestInfoManager;
import com.attack.model.InterestInfoModel;
import com.common.manager.CparamManager;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;
import com.framework.web.BaseController;

@Controller
@RequestMapping(value = "/management/customer/interest")
public class InterestInfoController extends BaseController{

	 private static String modelUrl="management/customer/interest/";
    
    @Autowired
	private InterestInfoManager interestInfoManager;
    
    @Autowired
	private CparamManager cparamManager;
    

	
    @RequestMapping("/{custId}/showList")
	public ModelAndView showList(HttpServletRequest request,HttpServletResponse response,@PathVariable("custId") String custId){
		
		ModelAndView modelView = new ModelAndView(modelUrl+"list");
		try {
			IData param = new DataMap();
			param.put("custId", (Integer.valueOf(custId)));
			List<Object> dataList = interestInfoManager.getObjectList(param);
			modelView.addObject("objList", dataList);
			modelView.addObject("custId", custId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelView;
	}
	

	/**
	 * 功能：执行批量列表更新操作
	 * @param menuInfoModel
	 * @return
	 */
	@RequestMapping("saveAll")
	public Object saveAll(HttpServletRequest request,HttpServletResponse response) {
		
		try{
			String custId  = request.getParameter("custId");
			String[] custIds = request.getParameterValues("custId");
			String[] intIds = request.getParameterValues("intID");
			String[] interestNames= request.getParameterValues("interestName");
			String[] identityNums = request.getParameterValues("identityNum");
			String[] linkPhones = request.getParameterValues("linkPhone");
			String[] bankNames = request.getParameterValues("bankName");
			String[] bankAccounts = request.getParameterValues("bankAccount");
			int opNum = 0;
			if(interestNames!=null){
				InterestInfoModel interestInfoModel;
				for (int i = 0; i < interestNames.length; i++) {
					interestInfoModel = new InterestInfoModel();
					if(StringUtils.isNotEmpty(intIds[i])){
						interestInfoModel.setIntID(Integer.valueOf(intIds[i]));
					}
					interestInfoModel.setCustId(Integer.valueOf(custIds[i]));
					interestInfoModel.setInterestName(interestNames[i]);
					interestInfoModel.setIdentityNum(identityNums[i]);
					interestInfoModel.setLinkPhone(linkPhones[i]);
					interestInfoModel.setBankName(bankNames[i]);
					interestInfoModel.setBankAccount(bankAccounts[i]);
					if(interestInfoModel.getIntID()!=null){
						 opNum =interestInfoManager.updateObject(interestInfoModel);
					}else{
						 opNum =interestInfoManager.insertObjectAndGetID(interestInfoModel);
					}
				}
			}
			if(opNum>0){
				return super.jsonReturn(response,200);
			}else{
				return super.jsonReturn(response,300);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage());
		}
	}

	/**
	 * 功能：执行更新操作
	 * @param menuInfoModel
	 * @return
	 */
	@RequestMapping("saveOrUpdate")
	public Object saveOrUpdate(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("interestInfoModel") InterestInfoModel interestInfoModel) {
		
		try{
			int opNum = 0;
			if(interestInfoModel.getIntID()!=null){
				 opNum =interestInfoManager.updateObject(interestInfoModel);
			}else{
				 opNum =interestInfoManager.insertObjectAndGetID(interestInfoModel);
			}
			if(opNum>0){
					return super.jsonReturn(response,200);
				}else{
					return super.jsonReturn(response,300);
				}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage());
		}
	}
	
	/**
	 * 功能：初始化添加页面
	 * @param interestInfoModel
	 * @return
	 */
	@RequestMapping("initAdd")
	public ModelAndView initAdd(@ModelAttribute("interestInfoModel") InterestInfoModel interestInfoModel) {
		ModelAndView modelView = new ModelAndView(modelUrl+"add");
		log.info("add is page");
		cparamManager.getCparamList(modelView, "'SYS_BANKNAME'");
		modelView.addObject("vo",interestInfoModel);
		return modelView;
	}
	
	/**
	 * 功能：初始化编辑页面
	 * @param intID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/{intID}/initEdit")
	public ModelAndView initEdit(@PathVariable("intID") String intID) {
		ModelAndView modelView = new ModelAndView(modelUrl+"update");
		cparamManager.getCparamList(modelView, "'SYS_BANKNAME'");
		
		IData dataMap = new DataMap();
		dataMap.put("intID", intID);
		InterestInfoModel interestInfoModel = (InterestInfoModel)interestInfoManager.getObjectInfo(dataMap);
		modelView.addObject("vo",interestInfoModel);
		return modelView;
	}
	
	/**
	 * 功能：执行插入操作
	 * @param interestInfoModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping("add")
	public Object insert(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("interestInfoModel") InterestInfoModel interestInfoModel) {
		try{
			int opNum = interestInfoManager.insertObjectAndGetID(interestInfoModel);
			if(opNum>0){
				return super.jsonReturn(response,200,true);
			}else{
				return super.jsonReturn(response,300,true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage(),true);
		}
	}
	
	/**
	 * 功能：执行更新操作
	 * @param interestInfoModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping("update")
	public Object update(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("interestInfoModel") InterestInfoModel interestInfoModel) {
		try{
			int opNum =interestInfoManager.updateObject(interestInfoModel);
			if(opNum>0){
				return super.jsonReturn(response,200,true);
			}else{
				return super.jsonReturn(response,300,true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage(),true);
		}
	}
	
	/**
	 * 功能：执行单记录删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/{intID}/delete")
	public Object delete(HttpServletResponse response,@PathVariable("intID") String intID) {
		try{
				IData dataMap = new DataMap();
				dataMap.put("intID", intID);
				int opNum =interestInfoManager.deleteObject(dataMap);
				if(opNum>0){
					return super.jsonReturn(response,200,false);
				}else{
					return super.jsonReturn(response,300,false);
				}
			} catch (Exception e) {
				return super.jsonReturn(response,300,e.getMessage(),false);
			}
	}
	
	
}